package com.toidep.fragment.others;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.api.ApiController;
import com.toidep.api.ParseJSON;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.entities.ModelEntity;
import com.toidep.entities.ProfileEntity;
import com.toidep.interfaces.ResponseListener;

public class ModelDetailProfileFragment extends BaseFragment {

	private View view;
	private MainActivity mainActivity;
	
	private ModelEntity modelEntity;
	
	public void setValues(ModelEntity modelEntity) {
		this.modelEntity = modelEntity;
	}
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_MODEL;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_model));
		mainActivity.mainFragment.showButtonSlide(false);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_model_detail_profile, container, false);

		mainActivity = (MainActivity) getActivity();
//
//		setTittle(getResources().getString(R.string.label_category_model));

		initComponents();
		
		requestData();

		setBaseValues();
		
		return view;
	}

	private ImageView modelImage, modelThumb;
	private TextView textview_model_name, modelName, modelBirthday, model_profile;
	private LinearLayout layoutLoading;
	
	private void initComponents() {
		textview_model_name = (TextView) view.findViewById(R.id.textview_model_name);
		modelImage = (ImageView) view.findViewById(R.id.modelImage);
		modelThumb  = (ImageView) view.findViewById(R.id.modelThumb);
		modelName = (TextView) view.findViewById(R.id.modelName);
		modelBirthday = (TextView) view.findViewById(R.id.modelBirthday);
		model_profile = (TextView) view.findViewById(R.id.model_profile);
		
		textview_model_name.setText(String.format(getString(R.string.text_model_profile_title), modelEntity.getName()));
		
		layoutLoading = (LinearLayout) view.findViewById(R.id.layoutLoading);
		layoutLoading.setVisibility(View.VISIBLE);
	}
	
	public static ModelDetailProfileFragment newInstance() {
		ModelDetailProfileFragment myFragment = new ModelDetailProfileFragment();

		return myFragment;
	}
	
	/**
	 * Goi API get list models
	 */
	private void requestData() {
		// Lay profile cua Nguoi mau
		ApiController.getModelProfile(mainActivity, modelEntity.getId(), new ResponseListener() {
			@Override
			public void processResponse(int inintstall) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void processResponse(String response) {
				if (response != null) {
					ProfileEntity profileEntity = ParseJSON.getProfile(response);
					setWidgetInfo(profileEntity);
				}
			}
		});
	}
	
	protected void setWidgetInfo(ProfileEntity profileEntity) {
		layoutLoading.setVisibility(View.GONE);
		
		int widthImage = ConfigUtility.size_full_width;
		int heightImage = (int) (widthImage * ConfigUtility.scale_size_top_image);
		
		int widthImageView = ConfigUtility.scrWidth;
		int heightImageView = (int) (widthImageView * ConfigUtility.scale_size_top_image);
		
		RelativeLayout.LayoutParams param1 = new RelativeLayout.LayoutParams(widthImageView, heightImageView);
		modelImage.setLayoutParams(param1);
		modelImage.setScaleType(ScaleType.FIT_XY);
		mainActivity.loadImage(modelImage, profileEntity.getBackground(), widthImage, heightImage, widthImageView, heightImageView);

		int imgSize = ConfigUtility.size_icon;
		RelativeLayout.LayoutParams param2 = new RelativeLayout.LayoutParams(imgSize, imgSize);
		modelThumb.setLayoutParams(param2);
		modelThumb.setScaleType(ScaleType.FIT_XY);
		mainActivity.loadImage(modelThumb, profileEntity.getIcon(), imgSize, imgSize, imgSize, imgSize);
		
		modelName.setText(profileEntity.getName());
		modelBirthday.setText(profileEntity.getBirthday());
		model_profile.setText(profileEntity.getDesc());
	}

}
